<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Title</title>
  </head>
  <body>
    <script>
      /*
          * function TreeNode(val, left, right) {
          *     this.val = (val===undefined ? 0 : val)
          *     this.left = (left===undefined ? null : left)
          *     this.right = (right===undefined ? null : right)
          * }
          *
          *
             -10
            / \
           3   2
          / \   \
         8   6   9
          */
      const tree = {
        val: -10,
        left: {
          val: 3,
          left: { val: 8, left: null, right: null },
          right: { val: 6, left: null, right: null },
        },
        right: {
          val: 2,
          left: null,
          right: { val: 9, left: null, right: null },
        },
      };
      var pathSum = function (root, targetSum) {
        const allPath = [];

        const getPathSum = (root, target, path) => {
          if (!root) return;
          target -= root.val;
          const newPath = [...path];
          newPath.push(root.val);

          if (!root.left && !root.right && target === 0) {
            allPath.push(newPath);
            return;
          }
          getPathSum(root.left, target, newPath);
          getPathSum(root.right, target, newPath);
        };
        getPathSum(root, targetSum, []);
        return allPath;
      };
      console.log(pathSum(tree, 1));
    </script>
  </body>
</html>
